package com.miaosmart.config;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.miaosmart.tools.GameRecordTools;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import static com.miaosmart.common.constant.SystemConstants.SERVICE_SYSTEM_PROMPT;

@Configuration
@RequiredArgsConstructor
public class AIConfig {


    private final StringRedisTemplate stringRedisTemplate;

    private final ObjectMapper objectMapper;

    @Bean
    public ChatMemory chatMemory() {
        return new RedisChatMemory(stringRedisTemplate, objectMapper);
    }


    @Bean
    ChatClient chatClient(ChatModel model, ChatMemory chatMemory, GameRecordTools gameRecordTools) {

        return ChatClient.builder(model)
                .defaultSystem(SERVICE_SYSTEM_PROMPT)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        new MessageChatMemoryAdvisor(chatMemory))
                .defaultTools(gameRecordTools)
                .build();
    }

} 